package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.ActivityType;
import util.enumeration.Language;
import util.enumhelper.LocalizedActivityType;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;

@FacesConverter(value = "activityTypeConverter", forClass = util.enumeration.ActivityType.class)
public class ActivityTypeConverter implements Converter {
    private WebLocalization localization;
    private String preferredLocale;
    
    public ActivityTypeConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }
    
    //getAsObject method converts a string to a an object
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        
        if (value == null || value.length() == 0) {
            return null;
        }
        
        /* NOTE:
         * currently, the checkbox list actually returns all the same string for the object part
         * but, I just split them into diff locales just in case we need them one day.
         */
        
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getActivityTypeAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getActivityTypeAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getActivityTypeAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getActivityTypeAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getActivityTypeAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null) {
            return null;
        }
        
        if (value instanceof ActivityType) {
            ActivityType activityType = (ActivityType) value;
            
            String localizedActivityType = "";
            
            // en_US ENGLISH, UNITED STATES
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
                localizedActivityType = getActivityTypeAsString_EN_US(activityType);
            }
            // en_SG ENGLISH, SINGAPORE
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
                localizedActivityType = getActivityTypeAsString_EN_SG(activityType);
            }
            // ms_MY MALAY, MALAYSIA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
                localizedActivityType = getActivityTypeAsString_MS_MY(activityType);
            }

            // zh_CN CHINESE, CHINA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
                localizedActivityType = getActivityTypeAsString_ZH_CN(activityType);
            }
            // other language...
            else {
                localizedActivityType = getActivityTypeAsString_EN_US(activityType);
            }

            return localizedActivityType;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.ActivityType");
        }
    }
    
    // simplified chinese (zh_CN)
    private Object getActivityTypeAsObject_ZH_CN(String value) {
        if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.BEAUTY;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.CLUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.CLUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.DINING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.DINING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.ENTERTAINMENT, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.ENTERTAINMENT;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.OTHER, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.OTHER;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.PUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.PUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.REST_N_RELAX, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.REST_N_RELAX;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SHOPPING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.SHOPPING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SIGHTSEEING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA)))) return ActivityType.SIGHTSEEING;
        else return null;
    }
    
    // default (en_US)
    private Object getActivityTypeAsObject_EN_US(String value) {
        if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.BEAUTY;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.CLUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.CLUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.DINING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.DINING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.ENTERTAINMENT, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.ENTERTAINMENT;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.OTHER, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.OTHER;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.PUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.PUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.REST_N_RELAX, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.REST_N_RELAX;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SHOPPING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.SHOPPING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SIGHTSEEING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US)))) return ActivityType.SIGHTSEEING;
        else return null;
    }
    
    // singapore english (en_SG)
    private static Object getActivityTypeAsObject_EN_SG(String value) {
        if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.BEAUTY;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.CLUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.CLUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.DINING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.DINING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.ENTERTAINMENT, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.ENTERTAINMENT;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.OTHER, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.OTHER;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.PUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.PUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.REST_N_RELAX, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.REST_N_RELAX;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SHOPPING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.SHOPPING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SIGHTSEEING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE)))) return ActivityType.SIGHTSEEING;
        else return null;
    }
    
    // bahasa malaysia (ms_MY)
    private Object getActivityTypeAsObject_MS_MY(String value) {
        if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.BEAUTY, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.BEAUTY;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.CLUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.CLUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.DINING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.DINING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.ENTERTAINMENT, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.ENTERTAINMENT;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.OTHER, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.OTHER;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.PUBBING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.PUBBING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.REST_N_RELAX, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.REST_N_RELAX;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SHOPPING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.SHOPPING;
        else if(value.equals(LocalizedActivityType.getLocalizedActivityTypeAsString(ActivityType.SIGHTSEEING, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY)))) return ActivityType.SIGHTSEEING;
        else return null;
    }
    
    // simplified chinese (zh_CN)
    private static String getActivityTypeAsString_ZH_CN(ActivityType activityType) {
        String localizedActivityType = LocalizedActivityType.getLocalizedActivityTypeAsString(activityType, SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA));
        return localizedActivityType;
    }
    
    // default (en_US)
    private static String getActivityTypeAsString_EN_US(ActivityType activityType) {
        String localizedActivityType = LocalizedActivityType.getLocalizedActivityTypeAsString(activityType, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US));
        return localizedActivityType;
    }
    
    // singapore english (en_SG)
    private static String getActivityTypeAsString_EN_SG(ActivityType activityType) {
        String localizedActivityType = LocalizedActivityType.getLocalizedActivityTypeAsString(activityType, SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE));
        return localizedActivityType;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getActivityTypeAsString_MS_MY(ActivityType activityType) {
        String localizedActivityType = LocalizedActivityType.getLocalizedActivityTypeAsString(activityType, SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY));
        return localizedActivityType;
    }
}